/*
 * fulfilment.cpp
 *
 *  Created on: 12.02.2012
 *      Author: Roma Jam
 */
#include "main.h"
#include "flashloader_support.h"
#include "keys.h"
#include "delay_util.h"
#include "fulfilment.h"
#include "usart.h"
#include "lcd_init.h"
#include "events.h"
#include "windows.h"
#include "motors.h"

extern uint32_t t;

void fulfill_t::F1(void) {
    if (window.pointer != 0) {
        window.pointer = window.pointer_previous;
        LCD.Clear();
        Active_window.Search(window.pointer);
        window.pointer_previous = 0;
        parametres.string_pointer_settings = 2;
        if (window.pointer == 3) {
            LCD.Clear_zone(0,5,0);
            Event.Settings_pointer_redraw();
        }
    }
} // F1

void fulfill_t::F2(void) {
    if (window.pointer == 4) { // Readme
        LCD.Clear();
        LCD.PrintString(5,3, " DFU ", true);
        LCD.PrintString(0,6, "Reset power for exit", false);
        USART_DeInit(USART1);
        Delay.ms(99);
        JumpToFlashLdr();
//        NVIC_SystemReset();
    }
} // F2

void fulfill_t::F3(void) {
    if (window.pointer == 5) { // Drive window
        motor_parametres.MotorUp_flag = false;
        MotorUp_Off();
        motor_parametres.MotorDown_flag = false;
        MotorDown_Off();
        motor_parametres.MotorRight_flag = false;
        MotorRight_Off();
        motor_parametres.MotorLeft_flag = false;
        MotorLeft_Off();
        Event.Motor_indicate();
    } // Drive window
} // F3

void fulfill_t::Left(void) {
    if (window.pointer == 2) { // Manual window
        MotorLeft_On();
        motor.motor_flag = true;
    }
    if (window.pointer == 3 ) { // Settings window
        if (parametres.string_pointer_settings == 2) {
            if ((parametres.cmd_sourse == Terminal) && (parametres.cmd_switch_pointer)) {
                parametres.cmd_sourse = LabServer;
                parametres.cmd_switch_pointer = false;
                LCD.Clear_zone(6,2,8);
                Event.Cmd_Sourse_switch();
            }
        }
        if (parametres.string_pointer_settings == 3) {
            if ((parametres.backlight == on) && (parametres.backlight_switch_pointer)) {
            parametres.backlight = off;
            parametres.backlight_switch_pointer = false;
            LCD.Clear_zone(11,3,5);
            }
            Event.Backlight_switch();
        }
        if (parametres.string_pointer_settings == 4) {
            if ((parametres.language == Russian) && (parametres.language_switch_pointer)) {
            parametres.language = English;
            parametres.language_switch_pointer = false;
            LCD.Clear();
            }
            Event.Language_switch();
            Active_window.Search(window.pointer);
        }
    } // Settings window

    if (window.pointer == 5) { // Drive window
             if (parametres.string_pointer_drive == 3) {
                 motor_parametres.MotorUp_flag = false;
                 MotorUp_Off();
             }
             if (parametres.string_pointer_drive == 4) {
                 motor_parametres.MotorDown_flag = false;
                 MotorDown_Off();
             }
             if (parametres.string_pointer_drive == 5) {
                 motor_parametres.MotorRight_flag = false;
                 MotorRight_Off();
             }
             if (parametres.string_pointer_drive == 6) {
                 motor_parametres.MotorLeft_flag = false;
                 MotorLeft_Off();
             }
             Event.Motor_indicate();
     } // Drive window
} // Left

void fulfill_t::Up(void) {
    if (window.pointer == 0) { // Main window
        parametres.string_pointer_main_old = parametres.string_pointer_main;
        parametres.string_pointer_main--;
        if (parametres.string_pointer_main < 2) parametres.string_pointer_main = 5;
        Event.Main_pointer_redraw();
    }
    if (window.pointer == 2) { // Manual window
        MotorUp_On();
        motor.motor_flag = true;
    }
    if (window.pointer == 3) { // Settings window
        parametres.string_pointer_settings_old = parametres.string_pointer_settings;
        parametres.string_pointer_settings--;
        if (parametres.string_pointer_settings < 2) parametres.string_pointer_settings = 5;
        Event.Settings_pointer_redraw();
    }
    if (window.pointer == 5) { // Drive window
        parametres.string_pointer_drive_old = parametres.string_pointer_drive;
        parametres.string_pointer_drive--;
        if (parametres.string_pointer_drive < 3) parametres.string_pointer_drive = 6;
        Event.Drive_pointer_redraw();
    }

} // Up

void fulfill_t::Right(void) {
    if (window.pointer == 2) { // Manual window
        MotorRight_On();
        motor.motor_flag = true;
    }
    if (window.pointer == 3) { // Settings window
        if (parametres.string_pointer_settings == 2) {
            if ((parametres.cmd_sourse == LabServer) && (!parametres.cmd_switch_pointer)) {
                parametres.cmd_sourse = Terminal;
                parametres.cmd_switch_pointer = true;
                LCD.Clear_zone(6,2,8);
                Event.Cmd_Sourse_switch();
            }
        }
        if (parametres.string_pointer_settings == 3) {
            if ((parametres.backlight == off) && (!parametres.backlight_switch_pointer)) {
                parametres.backlight = on;
                parametres.backlight_switch_pointer = true;
                LCD.Clear_zone(11,3,5);
            }
            Event.Backlight_switch();
        }
        if (parametres.string_pointer_settings == 4) {
            if ((parametres.language == English) && (!parametres.language_switch_pointer)) {
            parametres.language = Russian;
            parametres.language_switch_pointer = true;
            LCD.Clear();
            }
            Event.Language_switch();
            Active_window.Search(window.pointer);
        }
    } // Settings window

    if (window.pointer == 5) { // Drive window
            if (parametres.string_pointer_drive == 3) {
                motor_parametres.MotorUp_flag = true;
                MotorUp_On();
            }
            if (parametres.string_pointer_drive == 4) {
                motor_parametres.MotorDown_flag = true;
                MotorDown_On();
            }
            if (parametres.string_pointer_drive == 5) {
                motor_parametres.MotorRight_flag = true;
                MotorRight_On();
            }
            if (parametres.string_pointer_drive == 6) {
                motor_parametres.MotorLeft_flag = true;
                MotorLeft_On();
            }
            Event.Motor_indicate();
    } // Drive window
} // Right

void fulfill_t::Stop(void) {

} // Stop

void fulfill_t::Down(void) {
    if (window.pointer == 0) { // Main window
        parametres.string_pointer_main_old = parametres.string_pointer_main;
        parametres.string_pointer_main++;
        if (parametres.string_pointer_main > 5) parametres.string_pointer_main = 2;
        Event.Main_pointer_redraw();
    }
    if (window.pointer == 2) { // Manual window
        MotorDown_On();
        motor.motor_flag = true;
    }
    if (window.pointer == 3) { // Settings window
        parametres.string_pointer_settings_old = parametres.string_pointer_settings;
        parametres.string_pointer_settings++;
        if (parametres.string_pointer_settings > 5) parametres.string_pointer_settings = 2;
        Event.Settings_pointer_redraw();
    }
    if (window.pointer == 5) { // Drive window
        parametres.string_pointer_drive_old = parametres.string_pointer_drive;
        parametres.string_pointer_drive++;
        if (parametres.string_pointer_drive > 6) parametres.string_pointer_drive = 3;
        Event.Drive_pointer_redraw();
    }
} // Down

void fulfill_t::Start(void) {
    if (window.pointer == 0) { // Main window
        window.pointer_previous = window.pointer;
        if (parametres.string_pointer_main == 2) {
            window.pointer = 1;
        }
        if (parametres.string_pointer_main == 3) {
            window.pointer = 2;
        }
        if (parametres.string_pointer_main == 4) {
            window.pointer = 3;
        }
        if (parametres.string_pointer_main == 5) {
            window.pointer = 4;
        }
        LCD.Clear();
    } // Main window

    if (window.pointer == 3) { // Settings window
        if (parametres.string_pointer_settings == 5) {
            window.pointer_previous = window.pointer;
            window.pointer = 5;
            LCD.Clear();
        }
    } // Settings window

    Active_window.Search(window.pointer);
} // Start

void fulfill_t::None(void) {
    if ((window.pointer == 2) && motor.motor_flag) {
        Delay.ms(5);
        MotorUp_Off();
        MotorLeft_Off();
        MotorRight_Off();
        MotorDown_Off();
        motor.motor_flag = false;
    }
    return;
} // None
